<?php

class Schedule_model extends CI_Model {

    function load_schedule($student_id) {
        $this->db->join("country", "country.country_id = schedule.country_id");
        $query = $this->db->get_where("schedule", array("student_id" => $student_id));
        $schedule_list = $query->result();
        $schedule = array();
        foreach ($schedule_list as $row){
            $schedule_detail = $this->load_schedule_detail($row->schedule_id);
            $no_of_activity = count($schedule_detail['activities']);
            $row->no_of_activity = $no_of_activity;
            $schedule[] = $row;
        }
        return $schedule;
    }

    function load_country_list2() {
        $this->db->order_by('country_name', 'asc');
        $query = $this->db->get("country");
        return $query->result();
    }

    function load_country_list() {
        $this->db->order_by('country_name', 'asc');
        $query = $this->db->get("country");
        foreach ($query->result() as $row) {
            $country_list[$row->country_id] = $row->country_name;
        }
        return $country_list;
    }

    function do_create_schedule($student_id) {
        $this->load->helper('date');
        $data = array(
            'student_id' => $student_id,
            'sync_gcalendar' => $this->input->post('sync'),
            'country_id' => $this->input->post('country_id'),
            'schedule_name' => $this->input->post('name'),
            'schedule_remark' => $this->input->post('remark'),
            'create_date' => date('y-m-d'),
        );
        $query = $this->db->insert('schedule', $data);
        return $query;
    }

    function load_standard_activity($country_id) {
        $this->db->join("standard_activity sa", "sa.standard_activity_id = chsa.standard_activity_id");
        $query = $this->db->get_where("country_has_standard_activity chsa", array("chsa.country_id" => $country_id));
        return $query->result();
    }

    function load_schedule_detail($schedule_id) {
        $this->db->join("schedule", "schedule.schedule_id = sa.schedule_id");
        $query1 = $this->db->get_where("student_activity sa", array("sa.schedule_id" => $schedule_id));
        $result1 = $query1->result();
        $this->db->select("schedule_name, sync_gcalendar");
        $query2 = $this->db->get_where("schedule", array("schedule_id" => $schedule_id));
        $result2 = $query2->result();
        $result["activities"] = $result1;
        $result["schedule_name"] = $result2[0]->schedule_name;
        $result["sync_gcalendar"] = $result2[0]->sync_gcalendar;
        return $result;
    }

    function delete_schedule($row) {
        $this->db->delete("schedule", array("schedule_id" => $row));
    }

    function update_schedule($schedule_id) {
        $query = $this->db->get_where("schedule", array("schedule_id" => $schedule_id));
        return $query->result();
    }

    function do_update_schedule() {
        $schedule_id = $this->input->post('schedule_id');
        $data = array(
            'schedule_name' => $this->input->post('schedule_name'),
            'schedule_remark' => $this->input->post('remark'),
            'country_id' => $this->input->post('country_id'),
        );
        $query = $this->db->update('schedule', $data, array("schedule_id" => $schedule_id));
        return $query;
    }

    function create_activity() {
        $date1 = explode("/", $this->input->post('date1'));
        $date1_reverse = $date1[2] . "-" . $date1[1] . "-" . $date1[0];
        $date2 = explode("/", $this->input->post('date2'));
        $date2_reverse = $date2[2] . "-" . $date2[1] . "-" . $date2[0];
        $data = array(
            'schedule_id' => $this->input->post('schedule_id'),
            'activity_title' => $this->input->post('name'),
            'start_date' => date($date1_reverse),
            'end_date' => date($date2_reverse),
            'description' => $this->input->post('description'),
        );
        $query = $this->db->insert('student_activity', $data);
        return $query;
    }

    function keep_event_id($student_activity_id, $event_id) {
        $data = array('gcalendar_event_id' => $event_id);
        $this->db->update('student_activity', $data, array('student_activity_id' => $student_activity_id));
    }

    function update_activity($student_activity_id) {
        $this->db->join("schedule", "sa.schedule_id=schedule.schedule_id");
        $query = $this->db->get_where("student_activity sa", array("sa.student_activity_id" => $student_activity_id));
        $result = $query->result();
        if($result[0]->actual_start_date != null){
            $date = explode("-", $result[0]->actual_start_date);
            $date_reverse = $date[2] . "/" . $date[1] . "/" . $date[0];
            $result[0]->actual_start_date = $date_reverse;
        }
        if($result[0]->actual_end_date != null){
            $date = explode("-", $result[0]->actual_end_date);
            $date_reverse = $date[2] . "/" . $date[1] . "/" . $date[0];
            $result[0]->actual_end_date = $date_reverse;
        }
        return $result;
    }

    function do_update_activity() {
        $date1 = explode("/", $this->input->post('date1'));
        $date1_reverse = $date1[2] . "-" . $date1[1] . "-" . $date1[0];
        $date2 = explode("/", $this->input->post('date2'));
        $date2_reverse = $date2[2] . "-" . $date2[1] . "-" . $date2[0];
        $student_activity_id = $this->input->post('student_activity_id');
        $data = array(
            'activity_title' => $this->input->post('activity_title'),
            'actual_start_date' => $date1_reverse,
            'actual_end_date' => $date2_reverse,
            'description' => $this->input->post('description'),
        );
        $this->db->update('student_activity', $data, array("student_activity_id" => $student_activity_id));
    }

    function delete_activity($student_activity_id) {
        $this->db->delete("student_activity", array("student_activity_id" => $student_activity_id));
    }

    function take_standard_activity() {
        $date1 = explode("/", $this->input->post('date1'));
        $date1_reverse = $date1[2] . "-" . $date1[1] . "-" . $date1[0];
        $date2 = explode("/", $this->input->post('date2'));
        $date2_reverse = $date2[2] . "-" . $date2[1] . "-" . $date2[0];
        $data = array(
            'schedule_id' => $this->input->post('schedule_id'),
            'activity_title' => $this->input->post('activity_title'),
            'start_date' => $date1_reverse,
            'end_date' => $date2_reverse,
            'description' => $this->input->post('description'),
        );
        $query = $this->db->insert('student_activity', $data);
        return $query;
    }

    function load_activity_detail($student_activity_id) {
        $query = $this->db->get_where("student_activity", array("student_activity_id" => $student_activity_id));
        return $query->result();
    }

    function load_calendar_api() {
        $clientLibraryPath = APPPATH . '.\libraries\google_calendar_api';
        $dir = realpath($clientLibraryPath);
        $oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $dir);
        require_once 'Zend/Loader.php';
        Zend_Loader::loadClass('Zend_Gdata');
        Zend_Loader::loadClass('Zend_Gdata_AuthSub');
        Zend_Loader::loadClass('Zend_Gdata_Calendar');
    }

}

?>
